﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SachVietDLL;
using SachVietDLL.BLL;

namespace SachVietWebsite.Controllers
{
    public class BookController : Controller
    {
        private BookBLL bookBll = new BookBLL();
        private CategoryBLL categoryBll = new CategoryBLL();
        private PublisherBLL publisherBLL = new PublisherBLL();

        public ActionResult Index()
        {
            var bookdetails = bookBll.GetAllBooks();
            return View(bookdetails.ToList());
        }

        //
        // GET: /Book/Details/5

        public ActionResult Details(string id = null)
        {
            BookDetail bookdetail = bookBll.GetBookByID(id);
            if (bookdetail == null)
            {
                return HttpNotFound();
            }
            return View(bookdetail);
        }

        //
        // GET: /Book/Create

        public ActionResult Create()
        {
            ViewBag.category_ID = new SelectList(categoryBll.GetAllCategory(), "book_Category_ID", "book_Category_Description");
            ViewBag.publication_ID = new SelectList(publisherBLL.GetAllPublisher(), "publisher_ID", "publisher_Name");
            return View();
        }

        //
        // POST: /Book/Create

        [HttpPost]
        public ActionResult Create(BookDetail bookdetail)
        {
            if (ModelState.IsValid)
            {
                bookBll.Create(bookdetail);
                return RedirectToAction("Index");
            }

            ViewBag.category_ID = new SelectList(categoryBll.GetAllCategory(), "book_Category_ID", "book_Category_Description", bookdetail.category_ID);
            ViewBag.publication_ID = new SelectList(publisherBLL.GetAllPublisher(), "publisher_ID", "publisher_Name", bookdetail.publication_ID);
            return View(bookdetail);
        }

        //
        // GET: /Book/Edit/5

        public ActionResult Edit(string id = null)
        {
            BookDetail bookdetail = bookBll.GetBookByID(id);
            if (bookdetail == null)
            {
                return HttpNotFound();
            }
            ViewBag.category_ID = new SelectList(categoryBll.GetAllCategory(), "book_Category_ID", "book_Category_Description", bookdetail.category_ID);
            ViewBag.publication_ID = new SelectList(publisherBLL.GetAllPublisher(), "publisher_ID", "publisher_Name", bookdetail.publication_ID);
            return View(bookdetail);
        }

        //
        // POST: /Book/Edit/5

        [HttpPost]
        public ActionResult Edit(BookDetail bookdetail)
        {
            if (ModelState.IsValid)
            {
                bookBll.Edit(bookdetail);
                return RedirectToAction("Index");
            }
            ViewBag.category_ID = new SelectList(categoryBll.GetAllCategory(), "book_Category_ID", "book_Category_Description", bookdetail.category_ID);
            ViewBag.publication_ID = new SelectList(publisherBLL.GetAllPublisher(), "publisher_ID", "publisher_Name", bookdetail.publication_ID);
            return View(bookdetail);
        }

        //
        // GET: /Book/Delete/5

        public ActionResult Delete(string id = null)
        {
            BookDetail bookdetail = bookBll.GetBookByID(id);
            if (bookdetail == null)
            {
                return HttpNotFound();
            }
            return View(bookdetail);
        }

        //
        // POST: /Book/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            bookBll.Delete(id);
            return RedirectToAction("Index");
        }
    }
}